package com.easyjava.builder;

import com.easyjava.bean.Constants;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class BuildBase {
    public static void execute() {
        List<String> headerInfoList = new ArrayList<>();
        //生成日期枚举
        headerInfoList.add("package "+Constants.PACKAGE_ENUM);
        build(headerInfoList,"DateTimePatternEnum", Constants.PATH_ENUM);

        headerInfoList.clear();
        headerInfoList.add("package "+Constants.PACKAGE_UTILS);
        build(headerInfoList,"DateUtils", Constants.PATH_UTILS);

        //baseMapper
        headerInfoList.clear();
        headerInfoList.add("package "+Constants.PACKAGE_MAPPER);
        build(headerInfoList,"BaseMapper", Constants.PATH_MAPPER);

        //生成pageSize枚举
        headerInfoList.clear();
        headerInfoList.add("package "+Constants.PACKAGE_ENUM);
        build(headerInfoList,"PageSize", Constants.PATH_ENUM);

        //生成simplePage
        headerInfoList.clear();
        headerInfoList.add("package "+Constants.PACKAGE_QUERY);
        build(headerInfoList,"SimplePage", Constants.PATH_QUERY);

        //生成BaseQuery
        headerInfoList.clear();
        headerInfoList.add("package "+Constants.PACKAGE_QUERY);
        build(headerInfoList,"BaseQuery", Constants.PATH_QUERY);

        //PaginationResultVo
        headerInfoList.clear();
        headerInfoList.add("package "+Constants.PACKAGE_VO);
        build(headerInfoList,"PaginationResultVo", Constants.PATH_VO);

        //生成exception
        headerInfoList.clear();
        headerInfoList.add("package "+Constants.PACKAGE_EXCEPTION);
        headerInfoList.add("import "+Constants.PACKAGE_ENUM+".ResponseCodeEnum");
        build(headerInfoList,"BusinessException", Constants.PATH_EXCEPTION);


        //生成ResponseCodeEnum
        headerInfoList.clear();
        headerInfoList.add("package "+Constants.PACKAGE_ENUM);
        build(headerInfoList,"ResponseCodeEnum", Constants.PATH_ENUM);

        //生成ResponseVo
        headerInfoList.clear();
        headerInfoList.add("package "+Constants.PACKAGE_VO);
        build(headerInfoList,"ResponseVo", Constants.PATH_VO);

        //生成BaseController
        headerInfoList.clear();
        headerInfoList.add("package "+Constants.PACKAGE_CONTROLLER);
        headerInfoList.add("import "+Constants.PACKAGE_ENUM+".ResponseCodeEnum");
        headerInfoList.add("import "+Constants.PACKAGE_VO+".ResponseVo");
        build(headerInfoList,"ABaseController", Constants.PATH_CONTROLLER);


        //生成AGlobalExceptionHandlerController
        headerInfoList.clear();
        headerInfoList.add("package "+Constants.PACKAGE_CONTROLLER);
        headerInfoList.add("import "+Constants.PACKAGE_ENUM+".ResponseCodeEnum");
        headerInfoList.add("import "+Constants.PACKAGE_VO+".ResponseVo");
        headerInfoList.add("import "+Constants.PACKAGE_EXCEPTION+".BusinessException");
        build(headerInfoList,"AGlobalExceptionHandlerController", Constants.PATH_CONTROLLER);


    }

    private static void build(List<String> headerInfoList , String fileName, String outputPath) {
        File folder = new File(outputPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        File javaFile = new File(outputPath, fileName + ".java");

        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;

        InputStream in = null;
        InputStreamReader inr = null;
        BufferedReader bf = null;
        try {
            out = new FileOutputStream(javaFile);
            outw = new OutputStreamWriter(out, "utf-8");
            bw = new BufferedWriter(outw);

            String templatePath = BuildBase.class.getClassLoader().getResource("template/" + fileName + ".txt").getPath();
            in = new FileInputStream(templatePath);
            inr = new InputStreamReader(in, "utf-8");
            bf = new BufferedReader(inr);

            for (String head:headerInfoList){
                bw.write(head+";");
                bw.newLine();
                if (head.contains("package")){
                    bw.newLine();
                }
            }


            String lineInfo = null;
            while ((lineInfo = bf.readLine()) != null) {
                bw.write(lineInfo);
                bw.newLine();
            }
            bw.flush();

        } catch (Exception e) {
            log.info("生成基础类: {} 失败 : ", fileName, e);
        } finally {
            if (bf != null) {
                try {
                    bf.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (inr != null) {
                try {
                    inr.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
